# _*_ coding: utf-8 _*_
# @Time : 2024-01-20 22:22
# @Author : wf
import time

import requests

# __VIEWSTATE: HxUsCR5QYW4lAaS7cg3lHE45ss/tEpsUJFQ22nF3saXrCmTb2N9dYSKGiIbxoO2TGWCX1CAgsPZmbEcxWk5dE/mAj+xXVveqEwIrz4nij3C67zNizAQMkg3ZGO0robM0W6B5Xq8U0CoqfNKjzKLbdNWYOlo=
# __VIEWSTATEGENERATOR: C93BE1AE
# from: http://so.gushiwen.cn/user/collect.aspx
# email: 1766441627@qq.com
# pwd: Wangfeng1026
# code: omub
# denglu: 登录

# __VIEWSTATE __VIEWSTATEGENERATOR 在页面的源码中

from bs4 import BeautifulSoup

url = 'https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx'
resp = requests.get(url)
resp.encoding = 'utf-8'
# print(resp.text)

# 获取页面元素
soup = BeautifulSoup(resp.text, 'lxml')
state = soup.select('#__VIEWSTATE')[0].attrs.get('value')
state_2 = soup.select('#__VIEWSTATEGENERATOR')[0].attrs.get('value')
# print(state, state_2)

# 解析验证码
# check_url = 'https://so.gushiwen.cn/RandCode.ashx'
check_url = 'https://so.gushiwen.cn' + soup.select('#imgCode')[0].attrs.get('src')
# print(check_url)

# 保持是一个对象
session = requests.session()
resp_code = session.get(check_url)
# 二进制数据
content_code = resp_code.content
with open('code2.png', 'wb') as fp:
    fp.write(content_code)

# import urllib.request
# urllib.request.urlretrieve(check_url, filename='code.png')

time.sleep(1)
check_code = input("输入验证码：")
pwd = input("输入密码：")
login_url = 'https://so.gushiwen.cn/user/login.aspx?from=http%3a%2f%2fso.gushiwen.cn%2fuser%2fcollect.aspx'
data = {
    '__VIEWSTATE': state,
    '__VIEWSTATEGENERATOR': state_2,
    'from': 'http://so.gushiwen.cn/user/collect.aspx',
    'email': '1766441627@qq.com',
    'pwd': pwd,
    'code': check_code,
    'denglu': '登录',
}
response = session.post(login_url, data=data)
with open('gushiwen.html', 'w+', encoding='utf-8') as fp:
    fp.write(response.text)
